import { wxRequest } from '../../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    pageNo: 1,
    hasNextPage: false,
    list: [],
    keyword:'',
    address: [],
    pageData: {} 
  },

  // handleAudit(e) {
  //   var id = e.currentTarget.dataset.id
  //   _wxRequest.wxTonkenAjax({
  //     url: app.globalData.apiUrl + '/org/protected/bizOrg/applyAgent',
  //     method: 'POST',
  //     data: { id }
  //   }, (res) => {
  //     console.log(res)
  //     if(res) {
  //       wx.showToast({
  //         icon: 'none',
  //         title: '审核成功',
  //       })
  //       this.setData({
  //         list: []
  //       })
  //       this.getlist()
  //     }
  //   })
  // },

  // onShow: function () { 
  //   this.getlist()
  // },

  onLoad: function () {
    this.getlist()
  },

  //查询列表
  getlist: function () {
    _wxRequest.wxTonkenAjax({
      //url: app.globalData.apiUrl + '/org/protected/comm/beanCrud/org.AgentVO/find',
      url: app.globalData.apiUrl + '/org/protected/bizOrg/findChainList',
      method: 'POST',
      data:{
        keyword: this.data.keyword,
        pageNo: this.data.pageNo,
        disId: this.data.address.length > 2 ? this.data.address[2].code : '',
        cityId: this.data.address.length > 1 ? this.data.address[1].code : '',
        proId: this.data.address.length > 0 ? this.data.address[0].code : '',
      }
    }, (res) => {

      let list = res.list;
      if (this.data.pageNo !== 1) {
        list = this.data.list.concat(list);
      }

      this.setData({
        list: list,
        pageData: res.page,
        pageNo: res.page.pageNo,
        hasNextPage: res.page.hasNextPage,
      });

      console.log(res)

    })
  },


  //下拉刷新
  onPullDownRefresh: function () {
    this.refreshData();
    wx.stopPullDownRefresh()
  },

  refreshData: function () {
    this.setData({
      pageNo: 1
    })
    this.getlist();
  },

  //触底时，加载更多
  onReachBottom: function () {
    if (this.data.hasNextPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.getlist();
    }
  },

  //搜索关键词
  seachInputConfirm: function (e) {
    this.setData({
      pageNo: 1,
      keyword: e.detail.value
    })
    this.getlist();
  },
  
  seachBtn: function () {
    this.setData({
      pageNo: 1,
      keyword: this.data.keyword
    })
    this.getlist();
  },

  seachInputVal: function (e) {
    var inputval = e.detail.value;
    console.log(e)
    this.setData({
      keyword: inputval
    })
  },

  //清空
  clearInput: function () {
    this.setData({
      keyword: ''
    })
    this.getlist();
  },

 
  //编辑
  handleEdit: function (e) {
    var item = e.currentTarget.dataset.item
    wx.setStorageSync('chainItem', item)
    wx.navigateTo({
      url: '/pages/publish/chain_store/index',
    })
  },


  //删除
  handlDelec: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '提示',
      confirmText: '确定',
      cancelText: '取消',
      content: '确定删除该连锁总部',
      success: function (res) {
        if (res.confirm) {
          that.delectRequest(id)
        }
      }
    })
  },


  delectRequest: function (id) {
    var that = this 
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + "/org/protected/bizOrg/deleteChain",
      method: 'POST',
      data: { id }
    }, (res) => {
      console.log(res,'删除成功')
      wx.showToast({
        title: "删除成功",
      })
      setTimeout(()=>{
        that.getlist()
      },500)
    })
  },


  //********************筛选更多********************
  popupRight: function () {
    this.setData({
      showPopup: true
    })
  },

  popupCancel: function () {
    this.setData({
      showPopup: false
    })
  },
  //重置
  popupReset: function () {
    this.setData({
      disId: '',
      address: [],
      quyu: false
    })
  },


  popupConfirm: function () {
    this.setData({
      pageNo: 1
    })
    this.getlist();
    this.popupCancel();
  },


  chooseAddress: function () {
    this.setData({
      quyu: true
    })
  },
  addresscancel: function () {
    this.setData({
      quyu: false
    })
  },

  addressDetail: function (e) {
    this.setData({
      address: e.detail,
      quyu: false
    })
  },



  //********************筛选更多end********************//







})